package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import entity.Cinema;
import entity.Movie;
import util.ConnManager;

public class MovieDaoImpl implements MovieDao{
	//查看所有电影信息
	@Override
	public List<Movie> MovieAll() {
		List<Movie> movieList = new ArrayList<Movie>();
		try {
			Connection conn =ConnManager.getConnection();
			String sql = "select * from movie";
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Movie m =new  Movie();
				m.setMovieId(rs.getInt("movieId"));
				m.setName(rs.getString("name"));
				m.setClassId(rs.getString("classId"));
				m.setColor(rs.getString("color"));
				m.setDirector(rs.getString("Director"));
				m.setImg(rs.getString("img"));
				m.setIntroduce(rs.getString("introduce"));
				m.setLanguage(rs.getString("language"));
				m.setMainActor(rs.getString("mainActor"));
				m.setMovieTime(rs.getInt("movieTime"));
				m.setNation(rs.getString("nation"));
				m.setPrice(rs.getString("price"));
				m.setScore(rs.getString("score"));
				m.setStatus(rs.getString("status"));
				m.setAddTime(rs.getDate("addTime"));
				movieList.add(m);
				
			}
			
			rs.close();
			pst.close();
			conn.close();
			
			
		} catch (Exception e) {
			System.out.println("获取数据库时发生异常："+e.getMessage());
		}
		
		return movieList;
	}

	//查看所有影院信息
	@Override
	public List<Cinema> CinemaAll() {
		List<Cinema> cinemaList = new ArrayList<Cinema>();
		try {
			Connection conn = ConnManager.getConnection();
			String sql = "select * from cinema";
			PreparedStatement pst = conn.prepareStatement(sql);
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Cinema c = new Cinema();
				c.setCinemaId(rs.getInt("cinemaId"));
				c.setName(rs.getString("name"));
				c.setPhone(rs.getString("phone"));
				c.setAddr(rs.getString("addr"));
				c.setIntroduce(rs.getString("introduce"));
				cinemaList.add(c);
			}
			
			rs.close();
			pst.close();
			conn.close();
		} catch (Exception e) {
			System.out.println("获取数据库时发生异常："+e.getMessage());
		}
		return cinemaList;
	}

	@Override
	public List<Movie> StatusAll() {
		List<Movie> jjsyList = new ArrayList<Movie>();
		
		try {
			Connection conn =ConnManager.getConnection();
			String sql = "select * from movie where status='即将上映'";
			PreparedStatement pst = conn.prepareStatement(sql);
			
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Movie m =new  Movie();
				m.setMovieId(rs.getInt("movieId"));
				m.setName(rs.getString("name"));
				m.setClassId(rs.getString("classId"));
				m.setColor(rs.getString("color"));
				m.setDirector(rs.getString("Director"));
				m.setImg(rs.getString("img"));
				m.setIntroduce(rs.getString("introduce"));
				m.setLanguage(rs.getString("language"));
				m.setMainActor(rs.getString("mainActor"));
				m.setMovieTime(rs.getInt("movieTime"));
				m.setNation(rs.getString("nation"));
				m.setPrice(rs.getString("price"));
				m.setScore(rs.getString("score"));
				m.setStatus(rs.getString("status"));
				m.setAddTime(rs.getDate("addTime"));
				jjsyList.add(m);
				
			}
			
			rs.close();
			pst.close();
			conn.close();
			
			
		} catch (Exception e) {
			System.out.println("获取数据库时发生异常："+e.getMessage());
		}
		
		return jjsyList;
	}

	@Override
	public List<Movie> Status1All() {
		List<Movie> zzryList = new ArrayList<Movie>();
		
		try {
			Connection conn =ConnManager.getConnection();
			String sql = "select * from movie where status='正在热映'";
			PreparedStatement pst = conn.prepareStatement(sql);
			
			ResultSet rs = pst.executeQuery();
			while(rs.next()){
				Movie m =new  Movie();
				m.setMovieId(rs.getInt("movieId"));
				m.setName(rs.getString("name"));
				m.setClassId(rs.getString("classId"));
				m.setColor(rs.getString("color"));
				m.setDirector(rs.getString("Director"));
				m.setImg(rs.getString("img"));
				m.setIntroduce(rs.getString("introduce"));
				m.setLanguage(rs.getString("language"));
				m.setMainActor(rs.getString("mainActor"));
				m.setMovieTime(rs.getInt("movieTime"));
				m.setNation(rs.getString("nation"));
				m.setPrice(rs.getString("price"));
				m.setScore(rs.getString("score"));
				m.setStatus(rs.getString("status"));
				m.setAddTime(rs.getDate("addTime"));
				zzryList.add(m);
				
			}
			
			rs.close();
			pst.close();
			conn.close();
			
			
		} catch (Exception e) {
			System.out.println("获取数据库时发生异常："+e.getMessage());
		}
		
		return zzryList;
	}

}
